package com.wn.naturalbiorhythms;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.wn.naturalbiorhythms.bean.NaturalBirthdayAdapter;
import com.wn.naturalbiorhythms.tool.BirthdaySettingsSharePref;
import com.wn.naturalbiorhythms.tool.NaturalBiorhythmsAlgorithm;

public class BirthdaySettingsActivity extends Activity {
	
	private RadioGroup rg_model;
	private ListView settingsListView1;
	private NaturalBirthdayAdapter adapter1;
	private ListView settingsListView2;
	private NaturalBirthdayAdapter adapter2;
	private String []sexs;
	private BirthdaySettingsSharePref bsspref;
	private Button btn_other;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			//自定义title
			requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
			setContentView(R.layout.birthday_settings);
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.activity_title);
			initView();
			initTitleBar();
			initData();
			setListener();
			NaturalBiorhythmsApplication.addActivity(this);
		}
		
		private void initTitleBar() {
			Button btn_back = (Button)findViewById(R.id.button1_back);
			btn_back.setText(R.string.title_bar_btn1_back);
			TextView titleTxt =  (TextView)findViewById(R.id.textView1_title);
			titleTxt.setText(R.string.menu_setting_birthay_title);
			btn_other = (Button)findViewById(R.id.button2_other);
			btn_other.setText(R.string.title_bar_btn2_go2);
			
			if(bsspref.getModel() == 1){
				btn_other.setEnabled(true);
			}else{
				btn_other.setEnabled(false);
			}
			
			btn_back.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//setResult(Common.RESULTCODE_RESETBIRTHDAY);
					finish();
				}
			});
			btn_other.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(BirthdaySettingsActivity.this, DoubleTowPersonActivity.class);
					startActivity(intent);
				}
			});
		}
		
		private void initData() {

			byte model = bsspref.getModel();
			int id = R.id.RadioButton1_model_person1;
			if(model == 1){
				id = R.id.RadioButton2_model_person2;
				settingsListView2.setEnabled(true);
			}else{
				settingsListView2.setEnabled(false);
			}
			rg_model.check(id);
			sexs = new String[]{getString(R.string.sex1_text),getString(R.string.sex2_text)};
			//第一个用户
			long birthday1 = bsspref.getBirthday((byte)0);
			byte sex1 = bsspref.getSex((byte)0);
			
			List<Map<String, Object>> dataList1 = new ArrayList<Map<String,Object>>();
			//
			Map<String, Object> map_birthday1 = new HashMap<String, Object>();
			map_birthday1.put(NaturalBirthdayAdapter.key_title, getString(R.string.datepickerdialog_title));
			map_birthday1.put(NaturalBirthdayAdapter.key_content, NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.format(new Date(birthday1)));
			dataList1.add(map_birthday1);
			
			//
			Map<String, Object> map_sex1 = new HashMap<String, Object>();
			map_sex1.put(NaturalBirthdayAdapter.key_title, getString(R.string.datepickerdialog_sex_title));
			map_sex1.put(NaturalBirthdayAdapter.key_content, sexs[sex1]);
			dataList1.add(map_sex1);
			
			
			adapter1 = new NaturalBirthdayAdapter(this,dataList1);
			settingsListView1.setAdapter(adapter1);
			
			//第二个用户
			long birthday2 = bsspref.getBirthday((byte)1);
			byte sex2 = bsspref.getSex((byte)1);
			List<Map<String, Object>> dataList2 = new ArrayList<Map<String,Object>>();
			//
			Map<String, Object> map_birthday2 = new HashMap<String, Object>();
			map_birthday2.put(NaturalBirthdayAdapter.key_title, getString(R.string.datepickerdialog_title_other));
			map_birthday2.put(NaturalBirthdayAdapter.key_content, NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.format(new Date(birthday2)));
			dataList2.add(map_birthday2);
			
			//
			Map<String, Object> map_sex2 = new HashMap<String, Object>();
			map_sex2.put(NaturalBirthdayAdapter.key_title, getString(R.string.datepickerdialog_sex_title_other));
			map_sex2.put(NaturalBirthdayAdapter.key_content, sexs[sex2]);
			dataList2.add(map_sex2);
			
			adapter2 = new NaturalBirthdayAdapter(this,dataList2);
			settingsListView2.setAdapter(adapter2);
		}

		private void setListener() {
			
			settingsListView1.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					
					if(position == 0){
						final Map<String, Object> map = (Map<String, Object>)adapter1.getItem(0);
						String dateStr = map.get(NaturalBirthdayAdapter.key_content).toString();
						Calendar calendar = Calendar.getInstance();
						long todayLong = calendar.getTime().getTime();
						try {
							calendar.clear();
							calendar.setTime(NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.parse(dateStr));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						DatePickerDialog setting = new DatePickerDialog(BirthdaySettingsActivity.this,
								new OnDateSetListener() {
									@Override
									public void onDateSet(DatePicker view, int year,
											int monthOfYear, int dayOfMonth) {
										Calendar cal = Calendar.getInstance();
										cal.clear();
										cal.set(year, monthOfYear, dayOfMonth);
										//nbview.setBirthday(cal.getTimeInMillis());
										map.put(NaturalBirthdayAdapter.key_content, NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.format(cal.getTime()));
										adapter1.notifyDataSetChanged();
										bsspref.saveBirthday((byte)0, cal.getTime().getTime());
									}

								}, calendar.get(Calendar.YEAR), calendar
										.get(Calendar.MONTH), calendar
										.get(Calendar.DAY_OF_MONTH));
						setting.setTitle(R.string.datepickerdialog_title);
						setting.getDatePicker().setMaxDate(todayLong);
						setting.show();
					}else if(position == 1){
						
						byte sex = bsspref.getSex((byte)0);
						
						AlertDialog.Builder ab = new AlertDialog.Builder(BirthdaySettingsActivity.this);
						ab.setTitle(R.string.datepickerdialog_sex_title);
						ab.setSingleChoiceItems(sexs,sex,new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog, int which) {
								final Map<String, Object> map = (Map<String, Object>)adapter1.getItem(1);
								map.put(NaturalBirthdayAdapter.key_content, sexs[which]);
								adapter1.notifyDataSetChanged();
								bsspref.saveSex((byte)0, (byte)which);
								dialog.dismiss();
							}});
						ab.show();
					}
				
				}
				
			});
			

			
			settingsListView2.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					
					if(position == 0){
						final Map<String, Object> map = (Map<String, Object>)adapter2.getItem(0);
						String dateStr = map.get(NaturalBirthdayAdapter.key_content).toString();
						Calendar calendar = Calendar.getInstance();
						long todayLong = calendar.getTime().getTime();
						try {
							calendar.clear();
							calendar.setTime(NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.parse(dateStr));
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						DatePickerDialog setting = new DatePickerDialog(BirthdaySettingsActivity.this,
								new OnDateSetListener() {
									@Override
									public void onDateSet(DatePicker view, int year,
											int monthOfYear, int dayOfMonth) {
										Calendar cal = Calendar.getInstance();
										cal.clear();
										cal.set(year, monthOfYear, dayOfMonth);
										//nbview.setBirthday(cal.getTimeInMillis());
										map.put(NaturalBirthdayAdapter.key_content, NaturalBiorhythmsAlgorithm.SDF_DATE_yyyy_MM_dd.format(cal.getTime()));
										adapter2.notifyDataSetChanged();
										bsspref.saveBirthday((byte)1, cal.getTime().getTime());
									}

								}, calendar.get(Calendar.YEAR), calendar
										.get(Calendar.MONTH), calendar
										.get(Calendar.DAY_OF_MONTH));
						setting.setTitle(R.string.datepickerdialog_title_other);
						setting.getDatePicker().setMaxDate(todayLong);
						setting.show();
					}else if(position == 1){
						
						byte sex = bsspref.getSex((byte)1);
						
						AlertDialog.Builder ab = new AlertDialog.Builder(BirthdaySettingsActivity.this);
						ab.setTitle(R.string.datepickerdialog_sex_title_other);
						ab.setSingleChoiceItems(sexs,sex,new DialogInterface.OnClickListener(){

							@Override
							public void onClick(DialogInterface dialog, int which) {
								final Map<String, Object> map = (Map<String, Object>)adapter2.getItem(1);
								map.put(NaturalBirthdayAdapter.key_content, sexs[which]);
								adapter2.notifyDataSetChanged();
								bsspref.saveSex((byte)1, (byte)which);
								dialog.dismiss();
							}});
						
						ab.show();
					}
				
				}
				
			});
			
			rg_model.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					byte model = 0;
					if(checkedId == R.id.RadioButton2_model_person2){
						model = 1;
						settingsListView2.setEnabled(true);
						btn_other.setEnabled(true);
					}else{
						settingsListView2.setEnabled(false);
						btn_other.setEnabled(false);
					}
					
					bsspref.saveModel(model);
				}
			});
		
			
		}

		private void initView(){
			bsspref = BirthdaySettingsSharePref.getInstance(this);
			rg_model = (RadioGroup)findViewById(R.id.RadioGroup_model);
			
			settingsListView1 = (ListView)findViewById(R.id.ListView1_birthday_settings);
			settingsListView2 = (ListView)findViewById(R.id.ListView2_birthday_settings);
		}
		
		
		@Override
		protected void onDestroy() {
			
			super.onDestroy();
			/*if(rg_model.getCheckedRadioButtonId() == R.id.RadioButton2_model_person2){
				Intent intent = new Intent(BirthdaySettingsActivity.this, DoubleTowPersonActivity.class);
				startActivity(intent);
			}*/
			
		}
}
